package com.dzl.card.service.impl;

import com.dzl.base.service.BaseServiceImpl;
import com.dzl.card.dao.WelfareCardSecretMapper;
import com.dzl.card.pojo.WelfareCard;
import com.dzl.card.pojo.WelfareCardMoveRecord;
import com.dzl.card.pojo.WelfareCardSecret;
import com.dzl.card.pojo.dto.WelfareCardSecretDto;
import com.dzl.card.pojo.dto.WelfareMoveCardSecretAmountDto;
import com.dzl.card.pojo.query.WelfareCardSecretQuery;
import com.dzl.card.pojo.vo.WelfareCardExchangeSecretVo;
import com.dzl.card.pojo.vo.WelfareCardSecretVo;
import com.dzl.card.pojo.vo.WelfareUserCardSecretVo;
import com.dzl.card.service.WelfareCardMoveRecordService;
import com.dzl.card.service.WelfareCardSecretService;
import com.dzl.card.service.WelfareCardService;
import com.dzl.config.database.Master;
import com.dzl.config.exception.CommonException;
import com.dzl.constant.CommonConstants;
import com.dzl.constant.WelfareCardEnum;
import com.dzl.constant.WelfareExchangeEnum;
import com.dzl.exchange.pojo.WelfareExchange;
import com.dzl.exchange.pojo.WelfareExchangeSecret;
import com.dzl.exchange.service.WelfareExchangeSecretService;
import com.dzl.exchange.service.WelfareExchangeService;
import com.dzl.fee.pojo.WelfareFeeOrder;
import com.dzl.fee.pojo.WelfareFeeUserCustomers;
import com.dzl.fee.pojo.WelfareFeeUserSales;
import com.dzl.fee.pojo.query.WelfareSalesCustomersQuery;
import com.dzl.fee.service.WelfareFeeOrderService;
import com.dzl.userwx.pojo.WelfareUserWx;
import com.dzl.userwx.service.WelfareUserWxService;
import com.dzl.util.EmptyUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 卡密信息表(WelfareCardSecret)表服务实现类
 *
 * @author ChenLong
 * @since 2021-05-14 20:19:46
 */
@Service("welfareCardSecretService")
public class WelfareCardSecretServiceImpl extends BaseServiceImpl<WelfareCardSecret> implements WelfareCardSecretService {

    @Autowired
    private WelfareCardSecretMapper mapper;
    @Autowired
    private WelfareUserWxService welfareUserWxService;
    @Autowired
    private WelfareCardService welfareCardService;
    @Autowired
    private WelfareExchangeService welfareExchangeService;
    @Autowired
    private WelfareExchangeSecretService welfareExchangeSecretService;
    @Autowired
    private WelfareFeeOrderService welfareFeeOrderService;
    @Autowired
    private WelfareCardMoveRecordService welfareCardMoveRecordService;

    @Override
    public Map<String, Object> addUserCardAndPhoneBind(WelfareUserWx welfareUserWx, String secret) {
        Map<String, Object> resMap = new HashMap<>();
        // 校验卡密有效性（是否有效，是否已绑定，是否过期）
        Example welfareCardSecretExample = new Example(WelfareCardSecret.class);
        welfareCardSecretExample.createCriteria().andEqualTo("secret", secret);
        WelfareCardSecret welfareCardSecretResult = mapper.selectOneByExample(welfareCardSecretExample);
        // 能否找到该卡密，找不到去兑换券表找
        if (EmptyUtils.isEmpty(welfareCardSecretResult)) {
            Example exchangeSecretExample = new Example(WelfareExchangeSecret.class);
            exchangeSecretExample.createCriteria().andEqualTo("secret", secret);
            WelfareExchangeSecret welfareExchangeSecretRes = welfareExchangeSecretService.selectOneByExample(exchangeSecretExample);
            if (EmptyUtils.isEmpty(welfareExchangeSecretRes)) {
                resMap.put("code", 1025);
                resMap.put("msg", "找不到该卡密（兑换券）");
                return resMap;
            }
            WelfareExchange welfareExchange = welfareExchangeService.selectByPrimaryKey(welfareExchangeSecretRes.getGroupId());
            if(!welfareExchange.getEnterpriseId().equals(welfareUserWx.getEnterpriseId())){
                resMap.put("code", 1025);
                resMap.put("msg", "无法在小程序绑定该兑换券");
                return resMap;
            }
            if (EmptyUtils.isNotEmpty(welfareExchangeSecretRes.getStatus()) && welfareExchangeSecretRes.getIsActivate().equals(WelfareExchangeEnum.EXCHANGE_SECRET_ACTIVE_STATUS_BOUND.getCode())) {
                resMap.put("code", 1025);
                resMap.put("msg", "该兑换券已被绑定");
                return resMap;
            }
            if (EmptyUtils.isNotEmpty(welfareExchangeSecretRes.getStatus()) && welfareExchangeSecretRes.getIsActivate().equals(WelfareExchangeEnum.EXCHANGE_SECRET_ACTIVE_STATUS_USED.getCode())) {
                resMap.put("code", 1025);
                resMap.put("msg", "该兑换券已被使用");
                return resMap;
            }
            if (EmptyUtils.isNotEmpty(welfareExchangeSecretRes.getExpiredDate()) && welfareExchangeSecretRes.getExpiredDate().compareTo(new Date())==-1) {
                resMap.put("code", 1025);
                resMap.put("msg", "该兑换券已过期");
                return resMap;
            }
            if (EmptyUtils.isNotEmpty(welfareExchangeSecretRes.getIsActivate()) && welfareExchangeSecretRes.getIsActivate().equals(WelfareExchangeEnum.EXCHANGE_SECRET_ACTIVE_STATUS_FROZEN.getCode())) {
                resMap.put("code", 1025);
                resMap.put("msg", "该兑换券已被冻结");
                return resMap;
            }
            if (EmptyUtils.isNotEmpty(welfareExchangeSecretRes.getIsActivate()) && welfareExchangeSecretRes.getIsActivate().equals(WelfareExchangeEnum.EXCHANGE_SECRET_ACTIVE_STATUS_NOT_ACTIVE.getCode())) {
                resMap.put("code", 1025);
                resMap.put("msg", "该兑换券未激活");
                return resMap;
            }
            WelfareExchangeSecret welfareExchangeSecretUpd = new WelfareExchangeSecret();
            welfareExchangeSecretUpd.setId(welfareExchangeSecretRes.getId());
            //welfareExchangeSecretUpd.setIsBind(1);
            welfareExchangeSecretUpd.setBindUserId(welfareUserWx.getId());
            welfareExchangeSecretUpd.setUpdateTime(new Date());
            welfareExchangeSecretUpd.setBindTime(new Date());
            welfareExchangeSecretUpd.setIsActivate(WelfareCardEnum.CARD_SECRET_ACTIVE_STATUS_BOUND.getCode());
            int resCardSecret = welfareExchangeSecretService.updateByPrimaryKeySelective(welfareExchangeSecretUpd);
            if (resCardSecret < 1) {
                resMap.put("code", 1025);
                resMap.put("msg", "绑定兑换券失败");
                return resMap;
            }
            resMap.put("data", welfareExchangeSecretRes.getNumber());
        } else {
            WelfareCard welfareCard = welfareCardService.selectByPrimaryKey(welfareCardSecretResult.getCardId());
            if(!welfareCard.getEnterpriseId().equals(welfareUserWx.getEnterpriseId())){
                resMap.put("code", 1025);
                resMap.put("msg", "无法在小程序绑定该卡");
                return resMap;
            }
            if (EmptyUtils.isNotEmpty(welfareCardSecretResult.getStatus()) && welfareCardSecretResult.getIsActivate().equals(WelfareCardEnum.CARD_SECRET_ACTIVE_STATUS_BOUND.getCode())) {
                resMap.put("code", 1025);
                resMap.put("msg", "该卡密已被绑定");
                return resMap;
            }
            if (EmptyUtils.isNotEmpty(welfareCardSecretResult.getExpiredDate()) && welfareCardSecretResult.getExpiredDate().compareTo(new Date())==-1) {
                resMap.put("code", 1025);
                resMap.put("msg", "该卡密已过期");
                return resMap;
            }
            if (EmptyUtils.isNotEmpty(welfareCardSecretResult.getIsActivate()) && welfareCardSecretResult.getIsActivate().equals(WelfareCardEnum.CARD_SECRET_ACTIVE_STATUS_FROZEN.getCode())) {
                resMap.put("code", 1025);
                resMap.put("msg", "该卡密已被冻结");
                return resMap;
            }
            if (EmptyUtils.isNotEmpty(welfareCardSecretResult.getIsActivate()) && welfareCardSecretResult.getIsActivate().equals(WelfareCardEnum.CARD_SECRET_ACTIVE_STATUS_NOT_ACTIVE.getCode())) {
                resMap.put("code", 1025);
                resMap.put("msg", "该卡密未激活");
                return resMap;
            }
            // 更新卡密为已绑定，设置绑定用户为userId
            WelfareCardSecret welfareCardSecretUpd = new WelfareCardSecret();
            welfareCardSecretUpd.setId(welfareCardSecretResult.getId());
            //welfareCardSecretUpd.setIsBind(1);
            welfareCardSecretUpd.setBindUserId(welfareUserWx.getId());
            welfareCardSecretUpd.setUpdateTime(new Date());
            welfareCardSecretUpd.setBindTime(new Date());
            welfareCardSecretUpd.setIsActivate(WelfareCardEnum.CARD_SECRET_ACTIVE_STATUS_BOUND.getCode());
            int resCardSecret = mapper.updateByPrimaryKeySelective(welfareCardSecretUpd);
            if (resCardSecret < 1) {
                resMap.put("code", 1025);
                resMap.put("msg", "绑定卡密失败");
                return resMap;
            }
            resMap.put("data", welfareCardSecretResult.getNumber());
        }
        // 添加该用户绑定手机号
        int resUserWx = welfareUserWxService.updateByPrimaryKeySelective(welfareUserWx);
        if (resUserWx < 1) {
            resMap.put("code", 1026);
            resMap.put("msg", "绑定手机号失败");
            return resMap;
        }
        resMap.put("code", 1);
        resMap.put("msg", "success");
        return resMap;
    }

    @Override
    public Map<String, Object> addUserCardBind(String secret, Integer userId, Integer enterpriseId) {
        Map<String, Object> resMap = new HashMap<>();
        // 校验卡密有效性（是否有效，是否已绑定，是否过期）
        Example welfareCardSecretExample = new Example(WelfareCardSecret.class);
        welfareCardSecretExample.createCriteria().andEqualTo("secret", secret);
        WelfareCardSecret welfareCardSecretResult = mapper.selectOneByExample(welfareCardSecretExample);
        if (EmptyUtils.isEmpty(welfareCardSecretResult)) {
            Example exchangeSecretExample = new Example(WelfareExchangeSecret.class);
            exchangeSecretExample.createCriteria().andEqualTo("secret", secret);
            WelfareExchangeSecret welfareExchangeSecretRes = welfareExchangeSecretService.selectOneByExample(exchangeSecretExample);
            if (EmptyUtils.isEmpty(welfareExchangeSecretRes)) {
                resMap.put("code", 1025);
                resMap.put("msg", "找不到该卡密（兑换券）");
                return resMap;
            }
            WelfareExchange welfareExchange = welfareExchangeService.selectByPrimaryKey(welfareExchangeSecretRes.getGroupId());
            if(!welfareExchange.getEnterpriseId().equals(enterpriseId)){
                resMap.put("code", 1025);
                resMap.put("msg", "无法在小程序绑定该兑换券");
                return resMap;
            }
            if (EmptyUtils.isNotEmpty(welfareExchangeSecretRes.getStatus()) && welfareExchangeSecretRes.getIsActivate().equals(WelfareExchangeEnum.EXCHANGE_SECRET_ACTIVE_STATUS_BOUND.getCode())) {
                resMap.put("code", 1025);
                resMap.put("msg", "该兑换券已被绑定");
                return resMap;
            }
            if (EmptyUtils.isNotEmpty(welfareExchangeSecretRes.getStatus()) && welfareExchangeSecretRes.getIsActivate().equals(WelfareExchangeEnum.EXCHANGE_SECRET_ACTIVE_STATUS_USED.getCode())) {
                resMap.put("code", 1025);
                resMap.put("msg", "该兑换券已被使用");
                return resMap;
            }
            if (EmptyUtils.isNotEmpty(welfareExchangeSecretRes.getExpiredDate()) && welfareExchangeSecretRes.getExpiredDate().compareTo(new Date())==-1) {
                resMap.put("code", 1025);
                resMap.put("msg", "该兑换券已过期");
                return resMap;
            }
            if (EmptyUtils.isNotEmpty(welfareExchangeSecretRes.getIsActivate()) && welfareExchangeSecretRes.getIsActivate().equals(WelfareExchangeEnum.EXCHANGE_SECRET_ACTIVE_STATUS_FROZEN.getCode())) {
                resMap.put("code", 1025);
                resMap.put("msg", "该兑换券已被冻结");
                return resMap;
            }
            if (EmptyUtils.isNotEmpty(welfareExchangeSecretRes.getIsActivate()) && welfareExchangeSecretRes.getIsActivate().equals(WelfareExchangeEnum.EXCHANGE_SECRET_ACTIVE_STATUS_NOT_ACTIVE.getCode())) {
                resMap.put("code", 1025);
                resMap.put("msg", "该兑换券未激活");
                return resMap;
            }
            WelfareExchangeSecret welfareExchangeSecretUpd = new WelfareExchangeSecret();
            welfareExchangeSecretUpd.setId(welfareExchangeSecretRes.getId());
            //welfareExchangeSecretUpd.setIsBind(1);
            welfareExchangeSecretUpd.setBindUserId(userId);
            welfareExchangeSecretUpd.setUpdateTime(new Date());
            welfareExchangeSecretUpd.setBindTime(new Date());
            welfareExchangeSecretUpd.setIsActivate(WelfareCardEnum.CARD_SECRET_ACTIVE_STATUS_BOUND.getCode());
            int resCardSecret = welfareExchangeSecretService.updateByPrimaryKeySelective(welfareExchangeSecretUpd);
            if (resCardSecret < 1) {
                resMap.put("code", 1025);
                resMap.put("msg", "绑定兑换券失败");
                return resMap;
            }
        } else {
            WelfareCard welfareCard = welfareCardService.selectByPrimaryKey(welfareCardSecretResult.getCardId());
            if(!welfareCard.getEnterpriseId().equals(enterpriseId)){
                resMap.put("code", 1025);
                resMap.put("msg", "无法在小程序绑定该卡");
                return resMap;
            }
            if (EmptyUtils.isNotEmpty(welfareCardSecretResult.getStatus()) && welfareCardSecretResult.getIsActivate().equals(WelfareCardEnum.CARD_SECRET_ACTIVE_STATUS_BOUND.getCode())) {
                resMap.put("code", 1025);
                resMap.put("msg", "该卡密已被绑定");
                return resMap;
            }
            if (EmptyUtils.isNotEmpty(welfareCardSecretResult.getExpiredDate()) && welfareCardSecretResult.getExpiredDate().compareTo(new Date())==-1) {
                resMap.put("code", 1025);
                resMap.put("msg", "该卡密已过期");
                return resMap;
            }
            if (EmptyUtils.isNotEmpty(welfareCardSecretResult.getIsActivate()) && welfareCardSecretResult.getIsActivate().equals(WelfareCardEnum.CARD_SECRET_ACTIVE_STATUS_FROZEN.getCode())) {
                resMap.put("code", 1025);
                resMap.put("msg", "该卡密已被冻结");
                return resMap;
            }
            if (EmptyUtils.isNotEmpty(welfareCardSecretResult.getIsActivate()) && welfareCardSecretResult.getIsActivate().equals(WelfareCardEnum.CARD_SECRET_ACTIVE_STATUS_NOT_ACTIVE.getCode())) {
                resMap.put("code", 1025);
                resMap.put("msg", "该卡密未激活");
                return resMap;
            }
            // 更新卡密为已绑定，设置绑定用户为userId
            WelfareCardSecret welfareCardSecret = new WelfareCardSecret();
            welfareCardSecret.setId(welfareCardSecretResult.getId());
            //welfareCardSecret.setIsBind(1);
            welfareCardSecret.setBindUserId(userId);
            welfareCardSecret.setBindTime(new Date());
            welfareCardSecret.setIsActivate(WelfareCardEnum.CARD_SECRET_ACTIVE_STATUS_BOUND.getCode());
            int resCardSecret = mapper.updateByPrimaryKeySelective(welfareCardSecret);
            if (resCardSecret < 1) {
                resMap.put("code", 1025);
                resMap.put("msg", "绑定卡密失败");
                return resMap;
            }
        }
        resMap.put("code", 1);
        resMap.put("msg", "success");
        return resMap;
    }

    @Override
    public List<WelfareCardExchangeSecretVo> getCardExchangeInfoBySecret(String secret) {
        return mapper.getCardExchangeInfoBySecret(secret);
    }

    @Override
    public List<WelfareUserCardSecretVo> getUserCardBindList(Integer userId) {
        WelfareCardSecret welfareCardSecret = new WelfareCardSecret();
        welfareCardSecret.setBindUserId(userId);
        List<WelfareUserCardSecretVo> list =  mapper.getUserCardBindList(welfareCardSecret);
        if(EmptyUtils.isNotEmpty(list)){
            list.forEach(x->{
                x.setIsExpired(CommonConstants.NO);
                if(EmptyUtils.isNotEmpty(x.getExpiredDate()) && x.getExpiredDate().compareTo(new Date())==-1){
                    x.setIsExpired(CommonConstants.YES);
                }
            });
        }
        return list;
    }

    @Override
    public List<WelfareUserCardSecretVo> getUserCardBindListForOrderPay(Integer userId) {
        return mapper.getUserCardBindListForOrderPay(userId);
    }

    @Override
    public WelfareUserCardSecretVo getUserCardBindInfo(Integer cardSecretId) {
        return mapper.getUserCardBindInfo(cardSecretId);
    }

    @Override
    public PageInfo<WelfareCardSecretVo> selectWelfareCardSecretVoListPage(WelfareCardSecretQuery query) {
        /*String queryStr = query.getQueryStr();
        if (EmptyUtils.isNotEmpty(queryStr)) {
            if (queryStr.length() == 19) {
                query.setQueryStr(queryStr.replaceAll("-", ""));
            }
        }*/
        PageHelper.startPage(query);
        List<WelfareCardSecretVo> list = mapper.selectWelfareCardSecretVoListPage(query);
        if (EmptyUtils.isNotEmpty(list)) {
            list.forEach(welfareCardSecretVo -> {
                String secret = welfareCardSecretVo.getSecret();
                //字符串每4位加个“-”
                String secretHasLine = secret.replaceAll("(.{4})", "$1-");
                //去掉最后一个“-”
                secretHasLine = secretHasLine.substring(0, secretHasLine.length() - 1);
                welfareCardSecretVo.setSecretHasLine(secretHasLine);
            });
        }
        PageInfo<WelfareCardSecretVo> page = new PageInfo<WelfareCardSecretVo>(list);
        return page;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer updateBatchCardSecretByIds(WelfareCardSecretDto dto) {
        //先看多选的ids里面是否有不属于可变更为该状态的数据
        String ids = dto.getIds();
        List<WelfareCardSecret> welfareCardSecretList = mapper.selectByIds(ids);
        Integer isActivate = dto.getIsActivate();
        //是否激活 0：未激活，1：激活，2：已冻结 ，3批量解冻
        //如果是批量激活
        if (isActivate == 1) {
            //只要有一条数据的状态不能被激活，即状态不为“未激活”，不允许激活
            List<WelfareCardSecret> comp = welfareCardSecretList.stream().filter(x -> x.getIsActivate() != 0).collect(Collectors.toList());
            if(comp.size() > 0) {
                throw new CommonException("所选卡密包含不能激活的卡密，请重新选择！");
            }
            if(EmptyUtils.isNotEmpty(dto.getSalesId()) && EmptyUtils.isNotEmpty(dto.getCustomersId())){
                //生成业务员订单
                WelfareFeeOrder welfareFeeOrder = new WelfareFeeOrder();
                welfareFeeOrder.setEnterpriseId(dto.getEnterpriseId());
                welfareFeeOrder.setSalesUserId(dto.getSalesId());
                welfareFeeOrder.setCustomersId(dto.getCustomersId());
                welfareFeeOrder.setOrderType(1); //订单类型 1 福利卡 2 兑换券
                welfareFeeOrder.setCardId(welfareCardSecretList.get(0).getCardId());
                welfareFeeOrder.setQuantity(welfareCardSecretList.size());
                welfareFeeOrder.setCardAmount(dto.getAmount());
                welfareFeeOrder.setCardTotalAmount(dto.getAmount().multiply(new BigDecimal(welfareCardSecretList.size())));
                welfareFeeOrder.setCreaterName(dto.getCreaterName());
                welfareFeeOrderService.insertSelective(welfareFeeOrder);
                Integer feeOrderId = welfareFeeOrder.getId();
                dto.setFeeOrderId(feeOrderId);
            }
        }
        //如果是批量冻结
        if (isActivate == 2) {
            //只要有一条数据的状态不能被冻结，即状态不为“已激活”，不允许冻结
            List<WelfareCardSecret> comp = welfareCardSecretList.stream().filter(x -> x.getIsActivate() != 1 && x.getIsActivate() != 3).collect(Collectors.toList());
            if(comp.size() > 0) {
                throw new CommonException("所选卡密包含不能冻结的卡密，请重新选择！");
            }
        }
        //如果是批量解冻
        if (isActivate == 3) {
            //只要有一条数据的状态不能被解冻，即状态不为“已冻结”，不允许冻结
            List<WelfareCardSecret> comp = welfareCardSecretList.stream().filter(x -> x.getIsActivate() != 2).collect(Collectors.toList());
            if(comp.size() > 0) {
                throw new CommonException("所选卡密包含不能解冻的卡密，请重新选择！");
            }
        }
        return mapper.updateBatchCardSecretByIds(dto);
    }

    @Override
    public List<WelfareCardSecretVo> selectWelfareCardSecretVoListByIds(String ids) {
        return mapper.selectWelfareCardSecretVoListByIds(ids);
    }

    @Override
    public List<WelfareUserCardSecretVo> selectUserCardSecretVoListByUserId(Integer id) {
        return mapper.selectUserCardSecretVoListByUserId(id);
    }

    @Override
    public List<WelfareUserCardSecretVo> getUserWxCardInfoListNoExpiredById(Integer userId, Integer cardSecretId,
                                                                            Integer notIncludedCardSecretId) {
        return mapper.getUserWxCardInfoListNoExpiredById(userId, cardSecretId, notIncludedCardSecretId);
    }

    /*@Override
    public Integer updateIsExpiredByExpiredTime() {
        return mapper.updateIsExpiredByExpiredTime();
    }*/

    @Override
    public WelfareCardSecretVo queryWelfareCardSecretVoByNumberOrSecret(String number, String secret) {
        if (EmptyUtils.isNotEmpty(secret)) {
            if (secret.length() == 19) {
                secret = secret.replaceAll("-", "");
            }
        }
        WelfareCardSecretQuery query = new WelfareCardSecretQuery();
        query.setNumber(number);
        query.setSecret(secret);
        List<WelfareCardSecretVo> list = mapper.selectWelfareCardSecretVoListPage(query);
        if (EmptyUtils.isEmpty(list)) {
            throw new CommonException("查无此卡！");
        }
        WelfareCardSecretVo vo = list.get(0);
        //字符串每4位加个“-”
        String secretHasLine = vo.getSecret().replaceAll("(.{4})", "$1-");
        //去掉最后一个“-”
        secretHasLine = secretHasLine.substring(0, secretHasLine.length() - 1);
        vo.setSecretHasLine(secretHasLine);
        return vo;
    }

    @Override
    public Integer selectCountYearCard(String s) {
        return mapper.selectCountYearCard(s);
    }

    @Override
    public List<WelfareFeeUserSales> selectSalesByCardId(WelfareSalesCustomersQuery query) {
        return mapper.selectSalesByCardId(query);
    }

    @Override
    public List<WelfareFeeUserCustomers> selectCustomersByCardId(WelfareSalesCustomersQuery query) {
        return mapper.selectCustomersByCardId(query);
    }

    @Transactional(rollbackFor = Exception.class)
    @Master
    @Override
    public Integer moveCardSecretAmount(WelfareMoveCardSecretAmountDto dto) {
        Integer sourceId = dto.getSourceId();
        WelfareCardSecret sourceCardSecret = mapper.selectByPrimaryKey(sourceId);
        if(EmptyUtils.isEmpty(sourceCardSecret)) {
            throw new CommonException("转出卡不存在");
        }
        BigDecimal outCardOldAmount = sourceCardSecret.getRemainAmount();
        if(outCardOldAmount.compareTo(dto.getSourceAmount()) < 0) {
            throw new CommonException("剩余额度不够");
        }
        Integer targetId = dto.getTargetId();
        WelfareCardSecret targetCardSecret = mapper.selectByPrimaryKey(targetId);
        if(EmptyUtils.isEmpty(targetCardSecret)) {
            throw new CommonException("转入卡不存在");
        }
        if(!sourceCardSecret.getBindUserId().equals(targetCardSecret.getBindUserId())) {
            throw new CommonException("转入卡和转出卡用户不一致");
        }
        BigDecimal outAmount = dto.getSourceAmount();
        BigDecimal outCardNewAmount = outCardOldAmount.subtract(dto.getSourceAmount());
        sourceCardSecret.setRemainAmount(outCardNewAmount);
        sourceCardSecret.setPutOutAmount(sourceCardSecret.getPutOutAmount().add(outAmount));
        mapper.updateByPrimaryKeySelective(sourceCardSecret);
        BigDecimal scale = dto.getScale();
        BigDecimal targetAmount = dto.getSourceAmount().multiply(scale).setScale(2, RoundingMode.HALF_UP);
        BigDecimal inCardOldAmount = targetCardSecret.getRemainAmount();
        BigDecimal inCardNewAmount = targetCardSecret.getRemainAmount().add(targetAmount);
        targetCardSecret.setRemainAmount(inCardNewAmount);
        targetCardSecret.setPutInAmount(targetCardSecret.getPutInAmount().add(targetAmount));
        mapper.updateByPrimaryKeySelective(targetCardSecret);
        WelfareCardMoveRecord welfareCardMoveRecord = new WelfareCardMoveRecord();
        welfareCardMoveRecord.setUserId(sourceCardSecret.getBindUserId());
        welfareCardMoveRecord.setOutCardSecretId(sourceId);
        welfareCardMoveRecord.setOutCardOldAmount(outCardOldAmount);
        welfareCardMoveRecord.setOutAmount(outAmount);
        welfareCardMoveRecord.setOutCardNewAmount(outCardNewAmount);
        welfareCardMoveRecord.setScale(scale);
        welfareCardMoveRecord.setInCardSecretId(targetId);
        welfareCardMoveRecord.setInCardOldAmount(inCardOldAmount);
        welfareCardMoveRecord.setInAmount(targetAmount);
        welfareCardMoveRecord.setInCardNewAmount(inCardNewAmount);
        welfareCardMoveRecord.setUpdateName(dto.getUpdateName());
        return welfareCardMoveRecordService.insertSelective(welfareCardMoveRecord);
    }

    /*@Override
    public PageInfo<CardSecretPageVo> queryCardSecretListPage(WelfareCardSecretQuery secretQuery) {
        PageHelper.startPage(secretQuery);
        if(StringUtils.isNotBlank(secretQuery.getBatchNumber())){
            secretQuery.setBatchNumber(secretQuery.getBatchNumber().toUpperCase());
        }
        List<CardSecretPageVo> list= mapper.queryCardSecretListPage(secretQuery);
        if (EmptyUtils.isNotEmpty(list)) {
            list.forEach(welfareCardSecretVo -> {
                String secret = welfareCardSecretVo.getSecret();
                //字符串每4位加个“-”
                String secretHasLine = secret.replaceAll("(.{4})", "$1-");
                //去掉最后一个“-”
                secretHasLine = secretHasLine.substring(0, secretHasLine.length() - 1);
                welfareCardSecretVo.setSecret(secretHasLine);
            });
        }
        return new PageInfo<>(list);
    }*/
}
